package com.bootdo.system.controller;

import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.domain.ScanIpRangeDO;
import com.bootdo.system.service.ScanIpRangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2020-12-24 10:50:28
 */
 
@Controller
@RequestMapping("/system/scanIpRange")
public class ScanIpRangeController {
	@Autowired
	private ScanIpRangeService scanIpRangeService;

	@GetMapping()
	String ScanIpRange(){
	    return "system/scanIpRange/scanIpRange";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ScanIpRangeDO> scanIpRangeList = scanIpRangeService.list(query);
		int total = scanIpRangeService.count(query);
		PageUtils pageUtils = new PageUtils(scanIpRangeList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	String add(){
	    return "system/scanIpRange/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		ScanIpRangeDO scanIpRange = scanIpRangeService.get(id);
		model.addAttribute("scanIpRange", scanIpRange);
	    return "system/scanIpRange/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save( ScanIpRangeDO scanIpRange){

		//验证是否是同一个网段（不能跨网段。如：192.168.1.1-192.168.1.255）
		try {
			String ipStart = scanIpRange.getIpStart();
			String ipEnd = scanIpRange.getIpEnd();
			int prefix1 = ipStart.lastIndexOf(".");
			String prefix2 = ipStart.substring(0,prefix1);
			String prefix3 = ipEnd.substring(0,prefix1);
			int prefix4 = Integer.parseInt(ipStart.substring(prefix1 + 1,ipStart.length()));
			int prefix5 = Integer.parseInt(ipEnd.substring(prefix1 + 1,ipEnd.length()));
			if(!StringUtils.equals(prefix2,prefix3)) {
				return R.error("不能跨网段添加");
			}
			if(prefix5 <= prefix4 || prefix5 > 255) {
				return R.error("结束IP设置有误");
			}
		}catch (Exception e) {
			return R.error("结束IP设置有误");
		}

		if(scanIpRangeService.save(scanIpRange)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update( ScanIpRangeDO scanIpRange){
		//验证是否是同一个网段（不能跨网段。如：192.168.1.1-192.168.1.255）
		try {
			String ipStart = scanIpRange.getIpStart();
			String ipEnd = scanIpRange.getIpEnd();
			int prefix1 = ipStart.lastIndexOf(".");
			String prefix2 = ipStart.substring(0,prefix1);
			String prefix3 = ipEnd.substring(0,prefix1);
			int prefix4 = Integer.parseInt(ipStart.substring(prefix1 + 1,ipStart.length()));
			int prefix5 = Integer.parseInt(ipEnd.substring(prefix1 + 1,ipEnd.length()));
			if(!StringUtils.equals(prefix2,prefix3)) {
				return R.error("不能跨网段添加");
			}
			if(prefix5 <= prefix4 || prefix5 > 255) {
				return R.error("结束IP设置有误");
			}
		}catch (Exception e) {
			return R.error("结束IP设置有误");
		}

		scanIpRangeService.update(scanIpRange);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public R remove( Integer id){
		if(scanIpRangeService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] ids){
		scanIpRangeService.batchRemove(ids);
		return R.ok();
	}

	public static void main(String[] args) {
		String ip1 = "192.168.31.1";
		String ip2 = "192.168.31.255";
		int prefix = ip1.lastIndexOf(".");
		String prefix1 = ip1.substring(ip1.lastIndexOf("."));
		String prefix2 = ip1.substring(0,prefix);
		String prefix3 = ip1.substring(prefix + 1,ip1.length());
		String prefix4 = ip2.substring(prefix + 1,ip2.length());
		System.out.println(prefix);
		System.out.println(prefix1);
		System.out.println(prefix2);
		System.out.println(prefix3);
		System.out.println(prefix4);
	}

	/**
	 * 获取所有网段信息
	 * @param
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getScanIpRangeList")
	public List<ScanIpRangeDO> list(){
		//查询列表数据
		Map<String, Object> params = new HashMap<String, Object>();
		List<ScanIpRangeDO> scanIpRangeList = scanIpRangeService.list(params);
		return scanIpRangeList;
	}

}
